<?php

header('content-type:application:json;charset=utf-8');
header('Access-Control-Allow-Origin:*');  
header('Access-Control-Allow-Methods:POST');  
header('Access-Control-Allow-Headers:x-requested-with,content-type'); 


include("lib/cls_mysql.php");
include("lib/cls_image.php");

$db = new cls_mysql('localhost:3306', 'xcx_nmpfit_com', 'hSJENrw53pNhmcnn','xcx_nmpfit_com');

if(!$db){
	die('mysql no connection');
}

$mark = isset($_REQUEST['mark'])?$_REQUEST['mark']:'';
$size = isset($_REQUEST['size'])?$_REQUEST['size']:'';

if(empty($mark)){
	die('Params Error');
}



$sql = "select * from w_upload where token='{$mark}' limit 1";
$row = $db->getRow($sql);

//var_dump($row);

$image = new cls_image();


//header("Content-type: image/jpeg");   
if(!empty($size)){
	$fg = explode('x',$size);
	if(count($fg)<2){
		$wid = intval($fg[0]);
		if($wid>0){
			$hei = $wid;
		}
		else{
			create_img($row['path']);
		}
	}
	else{
		$wid = intval($fg[0]);
		$hei = intval($fg[1]);
	}
	
	if($wid>2040 || $hei>2040){
		die('大小不能超过2040像素');
	}
	
	$image->make_thumb($row['path'],$wid,$hei);
}
else{
	create_img($row['path']);
}

function create_img($path){
	$ext = substr(strrchr($path,'.'),1); 

	if($ext=='jpg'){
		$image = imagecreatefromjpeg($path);
		imagejpeg($image);  
	}
	elseif($ext=='png'){
		$image = imagecreatefrompng($path);
		imagepng($image);  
	}
	elseif($ext=='png'){
		$image = imagecreatefromgif($path);
		imagegif($image);  
	}
	else{
		die('create image fail');
		return false;
	}
	
	imagedestroy($image);
	$image = null;
}
